package com.wzx.selfuse.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wzx.selfuse.entity.PlanCust;
import com.wzx.selfuse.entity.PlanOrder;
import com.wzx.selfuse.entity.PlanPro;
import com.wzx.selfuse.service.impl.Plan_ICustServiceImpl;
import com.wzx.selfuse.service.impl.Plan_IOrderServiceImpl;
import com.wzx.selfuse.service.impl.Plan_IProServiceImpl;
import com.wzx.selfuse.vo.AjaxEntityResult;
import com.wzx.selfuse.vo.PlanOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author Liu Tenda
 * @date 2022年 12月11日 16:27:17
 * Introduction:
 */
@Controller
@RequestMapping("/order")
public class Plan_OrderController {
    @Autowired
    Plan_IOrderServiceImpl plan_iOrderService;
    @Autowired
    Plan_ICustServiceImpl plan_iCustService;
    @Autowired
    Plan_IProServiceImpl plan_iProService;
    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //    订单列表
    @RequestMapping("/orderList")
    public String getOrderList(HttpServletRequest req){
        Integer page=1;
        Integer num=5;
        if (req.getParameter("page")!=null){
            page = Integer.parseInt(req.getParameter("page"));
        }
        PageHelper.startPage(page,num);
        List<PlanOrderVo> orderList = plan_iOrderService.getOrderList();
        for (PlanOrderVo eve:orderList
             ) {
            if (eve.getOrderTime()!=null){
            String format = simpleDateFormat.format(eve.getOrderTime());
            eve.setOrderTimeVo(format);
            }
            if (eve.getReqTime()!=null){
            String format1 = simpleDateFormat.format(eve.getReqTime());
            eve.setReqTimeVo(format1);
            }
        }
        PageInfo<PlanOrderVo> of=PageInfo.of(orderList);
        req.setAttribute("orderList",of.getList());
        req.setAttribute("orderListOf",of);
        return "plan_order";
    }

    //    跳转添加订单页面
    @RequestMapping("/plan_order_add")
    public String OrderAdd(HttpServletRequest req){
        List<PlanCust> custList = plan_iCustService.getCustList();
        List<PlanPro> proList = plan_iProService.getProList();
        req.setAttribute("currentCust",custList);
        req.setAttribute("currentPro",proList);
        return "plan_order_add";
    }

    //    添加订单
    @PostMapping("/addOrder")
    @ResponseBody
    public AjaxEntityResult addPro(@RequestBody PlanOrder planOrder){
        AjaxEntityResult ajaxEntityResult = new AjaxEntityResult();
        PlanOrder orderByOrderNumber = plan_iOrderService.getOrderByOrderNumber(planOrder.getOrderNumber());
        Date orderTime = planOrder.getOrderTime();
        Date reqTime = planOrder.getReqTime();
        if (orderByOrderNumber!=null) {
            ajaxEntityResult.setCode(400);
            ajaxEntityResult.setMsg("编号已存在");
        } else {
            if (orderTime.getTime()-reqTime.getTime()>0){
                ajaxEntityResult.setCode(500);
                ajaxEntityResult.setMsg("要求时间不应早于订购时间");
            }else {
                ajaxEntityResult.setCode(200);
                ajaxEntityResult.setMsg("添加成功");
                plan_iOrderService.addOrder(planOrder);
            }
        }
        return ajaxEntityResult;
    }

    //    删除订单
    @PostMapping("/deleteOrder")
    @ResponseBody
    public AjaxEntityResult deleteOrder(@RequestBody String orderNumber){
        int i = plan_iOrderService.deleteOrder(orderNumber);
        AjaxEntityResult ajaxEntityResult=new AjaxEntityResult();
        if (i==0){
            ajaxEntityResult.setCode(400);
            ajaxEntityResult.setMsg("删除时发生错误");
        }
        if (i==1){
            ajaxEntityResult.setCode(200);
            ajaxEntityResult.setMsg("删除成功");
        }
        return ajaxEntityResult;
    }

    //    删除选中
    @PostMapping("/deleteChecked")
    @ResponseBody
    public AjaxEntityResult deleteChecked(@RequestBody List<String> orderNumber){
        Iterator<String> iterator = orderNumber.iterator();
        int a=0;//用于计数批量删除了多少条
        while (iterator.hasNext()){
            String next = iterator.next();
            int i = plan_iOrderService.deleteOrder(next);
            if (i==1){
                a++;
            }
        }
        AjaxEntityResult ajaxEntityResult=new AjaxEntityResult();
        if (a>0){
            ajaxEntityResult.setCode(200);
            ajaxEntityResult.setMsg("成功删除"+a+"条数据");
        }else {
            ajaxEntityResult.setCode(400);
            ajaxEntityResult.setMsg("删除失败，未选择用户或用户不存在");
        }
        return ajaxEntityResult;
    }

    //    跳转到订单要求页面
    @RequestMapping("/plan_order_introduction")
    public String proIntroduction(HttpServletRequest req){
        String orderNumber = req.getParameter("orderNumber");
        PlanOrder order = plan_iOrderService.getOrderByOrderNumber(orderNumber);
        String orderReq = order.getOrderReq();
        req.setAttribute("orderReq",orderReq);
        return "plan_order_introduction";
    }

//    跳转到订单编辑页面
    @RequestMapping("/plan_order_edit")
    public String proEdit(HttpServletRequest req){
        String orderNumber = req.getParameter("orderNumber");
        PlanOrderVo order = plan_iOrderService.getOrderByOrderNumber(orderNumber);
        if (order.getOrderTime()!=null){
            order.setOrderTimeVo(simpleDateFormat.format(order.getOrderTime()));
        }
        if (order.getReqTime()!=null){
            order.setReqTimeVo(simpleDateFormat.format(order.getReqTime()));
        }
        List<PlanCust> custList = plan_iCustService.getCustList();
        List<PlanPro> proList = plan_iProService.getProList();
        req.setAttribute("currentCust",custList);
        req.setAttribute("currentPro",proList);
        req.setAttribute("order",order);
        return "plan_order_edit";
    }

//    订单编辑
    @RequestMapping("/editOrder")
    @ResponseBody
    public AjaxEntityResult editPro(@RequestBody PlanOrder planOrder){
        Date orderTime = planOrder.getOrderTime();
        Date reqTime = planOrder.getReqTime();
        AjaxEntityResult result=new AjaxEntityResult();
        if (orderTime.getTime()-reqTime.getTime()>0){
            result.setCode(500);
            result.setMsg("要求日期不应早于订购日期");
        }else {
            int i = plan_iOrderService.editOrder(planOrder);
            if (i==0){
                result.setCode(400);
                result.setMsg("修改失败");
            }
            if (i==1){
                result.setCode(200);
                result.setMsg("修改成功");
            }
        }
        return result;
    }

//    按条件搜索订单
    @RequestMapping("/checkOrder")
    public String checkPro(String controller,String searchOrder,HttpServletRequest req){
        List<PlanOrderVo> order=new ArrayList<>();
        switch (controller){
            case "1":
                PlanOrderVo checkedorder = plan_iOrderService.getOrderByOrderNumber(searchOrder);
                order.add(checkedorder);
                break;
            case "2":
                order = plan_iOrderService.getOrderByCustName(searchOrder);
                break;
            case "3":
                order = plan_iOrderService.getOrderByProName(searchOrder);
                break;
            default:
                req.setAttribute("msg","找不到符合条件的产品");
        }
        req.setAttribute("orderList",order);
        return "plan_order";
    }
}